package springbootorderrabbitmqproducer.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.*;

import java.util.HashMap;

/**
 * @ClassName DirectMqConfiguration
 * @Date 2021/6/8 0008 上午 9:15
 * @Author Administrator
 * @Description TODO
 * Version 1.0
 */
@Configuration
public class DirectMqConfiguration {

    /**
     * 交换机名字
     */
    String exchangeName = "direct_pay_exchange";
    /**
     * 队列名
     */
    String smsDirectQueueName = "sms_direct_queue";
    String emailDirectQueueName = "email_direct_queue";
    String weChatDirectQueueName = "weChat_direct_queue";

    /**
     * 1.创建交换机
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(exchangeName, true, false);
    }

    /**
     * 创建队列
     *
     * @return
     */
    @Bean
    public Queue smsDirectQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",9000); //五秒  int 类型
        args.put("x-dead-letter-exchange","dead_direct_exchange");
        args.put("x-dead-letter-routing-key","dead");
        return new Queue(smsDirectQueueName, true,false,false,args);
    }

    @Bean
    public Queue emailDirectQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",9000); //五秒  int 类型
        args.put("x-dead-letter-exchange","dead_direct_exchange");
        args.put("x-dead-letter-routing-key","dead");
        return new Queue(emailDirectQueueName, true,false,false,args);
    }

    @Bean
    public Queue weChatDirectQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",9000); //五秒  int 类型
        args.put("x-dead-letter-exchange","dead_direct_exchange");
        args.put("x-dead-letter-routing-key","dead");
        return new Queue(weChatDirectQueueName, true,false,false,args);
    }

    /**
     * 交换机绑定队列设置路由key
     * @return
     */
    @Bean
    public Binding smsTopicBinding() {
        return BindingBuilder.bind(smsDirectQueue()).to(directExchange()).with("sms");
    }

    @Bean
    public Binding emailTopicBinding() {

        return BindingBuilder.bind(emailDirectQueue()).to(directExchange()).with("email");
    }

    @Bean
    public Binding weChatTopicBinding() {
        return BindingBuilder.bind(weChatDirectQueue()).to(directExchange()).with("weChat");
    }

}
